package androidgame.framework.impl;

import java.util.ArrayList;

import java.util.List;


import android.view.View;
import android.view.View.OnTouchListener;

import androidgame.framework.Pool;
import androidgame.framework.Input.Gesture;
import androidgame.framework.Input.TouchEvent;
import androidgame.framework.Pool.PoolObjectFactory;

 // Interface for touch, will be extended into MultiTouch and SingleTouch.
// @author dicarlo2

public abstract class TouchHandler implements OnTouchListener {
	
    Pool<TouchEvent> touchEventPool;
    List<TouchEvent> touchEvents = new ArrayList<TouchEvent>();
    List<TouchEvent> touchEventsBuffer = new ArrayList<TouchEvent>();
    GestureHandler gestureHandler;
    float scaleX;
    float scaleY;
    
    public TouchHandler(View view, float scaleX, float scaleY){
        PoolObjectFactory<TouchEvent> factory = new PoolObjectFactory<TouchEvent>() {
            @Override
            public TouchEvent createObject() {
                return new TouchEvent();
            }
        };
        touchEventPool = new Pool<TouchEvent>(factory, 100);
        view.setOnTouchListener(this);
        
        gestureHandler = new GestureHandler(this, scaleX, scaleY);

        this.scaleX = scaleX;
        this.scaleY = scaleY;
    }
    
    public abstract boolean isTouchDown(int pointer);
    
    public abstract int getTouchX(int pointer);
    
    public abstract int getTouchY(int pointer);
    
    public List<TouchEvent> getTouchEvents() {
        synchronized (this) {
            int len = touchEvents.size();
            for (int i = 0; i < len; i++)
                touchEventPool.free(touchEvents.get(i));
            touchEvents.clear();
            touchEvents.addAll(touchEventsBuffer);
            touchEventsBuffer.clear();
            return touchEvents;
        }
    }
    
    public List<Gesture> getGestures() {
    	return gestureHandler.getGestures();
    }
}
